package cn.likun.kit;
import java.util.Map;
/**
 * 获取线程一步返回值
 */
public class SyncValue implements Runnable {

	private Callable callable;
	private Map<String,Object> param;
	private Object value;
	private boolean runOver = false;
	private boolean runSuccess = true;
	
	public SyncValue(Callable callable, Map<String,Object> param) {
		this.callable = callable;
		this.param = param;
	}
	
	@Override
	public void run() {
		try {
			value = callable.call(param);
		} catch (Exception e) {
			runSuccess = false;
			value = e ;
			Logger.error(getClass(), "线程回调异常：",e);
		}
		runOver = true;
	}
	/**
	 * <p>描述: 获取线程执行的返回值,此方法将导致线程阻塞</p>
	 * @return 异步返回值
	 * @throws Exception
	 */
	public Object getValue() throws Exception {
		while (!runOver) {
			Thread.sleep(1000);
		}
		if (!runSuccess) {
			Exception e = (Exception) value;
			throw e;
		}
		return value;
	}

}
